﻿using GalaSoft.MvvmLight.Messaging;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MVBuddy.Messages;
using MVBuddy.ViewModel;
using System;
using System.Windows;
using System.Windows.Navigation;

namespace MVBuddy
{
    public partial class InfoPage : PhoneApplicationPage
    {
        public InfoPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            Messenger.Default.Register<LoginSuccessfulMessage>(this, LoginSuccessfulMessageReceived);
            Messenger.Default.Register<LoginFailedMessage>(this, LoginFailedMessageReceived);

            this.ToggleLoadingElements(true);
            Messenger.Default.Send<RequestLoginMessage>(new RequestLoginMessage());

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            Messenger.Default.Unregister<LoginSuccessfulMessage>(this, LoginSuccessfulMessageReceived);
            Messenger.Default.Unregister<LoginFailedMessage>(this, LoginFailedMessageReceived);

            base.OnNavigatedFrom(e);
        }

        private void Refresh_Click(object sender, System.EventArgs e)
        {
            this.ToggleLoadingElements(true);

            Messenger.Default.Send<RequestLoginMessage>(new RequestLoginMessage());
        }

        private void Settings_Click(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/SettingsPage.xaml", UriKind.Relative));
        }

        private void About_Click(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/AboutPage.xaml", UriKind.Relative));
        }

        private void LoginSuccessfulMessageReceived(LoginSuccessfulMessage message)
        {
            this.ToggleLoadingElements(false);
        }

        private void LoginFailedMessageReceived(LoginFailedMessage message)
        {
            MessageBox.Show(message.Content);

            NavigationService.Navigate(new Uri("/View/MainPage.xaml", UriKind.Relative));
            NavigationService.RemoveBackEntry();
        }

        private void NotificationTextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Messenger.Default.Send<RequestWebBrowserMessage>(new RequestWebBrowserMessage("http://m.mediavida.com/notificaciones"));
        }

        private void FavoriteTextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Messenger.Default.Send<RequestWebBrowserMessage>(new RequestWebBrowserMessage("http://m.mediavida.com/foro/favoritos"));
        }

        private void MessageTextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Messenger.Default.Send<RequestWebBrowserMessage>(new RequestWebBrowserMessage("http://m.mediavida.com/mensajes"));
        }

        private void ToggleLoadingElements(bool loading)
        {
            this.LoadingStackPanel.Visibility = loading == true ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            this.InfoStackPanel.Visibility = loading == false ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }
    }
}